const { pool } = require("../../db");

exports.getFeedbackList = async (limit, offset, type) => {
  let sql = `SELECT * from feedback `;
  let count = "SELECT COUNT(*) as count from feedback";

  if (type == 1) {
    sql += " WHERE answer IS NOT NULL ";
    count += " WHERE answer IS NOT NULL ";
  } else if (type == 2) {
    sql += " WHERE answer IS NULL ";
    count += " WHERE answer IS NULL ";
  }
  sql += `LIMIT ${limit} OFFSET ${offset}`;

  try {
    const [rows] = await pool.query(sql);
    const [countRows] = await pool.query(count);

    return { code: 200, data: rows, total: countRows[0].count };
  } catch (error) {
    return { code: 500, msg: "获取失败" };
  }
};

exports.updateFeedbackAnswer = async (id, answer) => {
  const sql = `UPDATE feedback SET answer = ? WHERE id = ?`;
  try {
    const [rows] = await pool.query(sql, [answer, id]);
    return { code: 200, data: rows };
  } catch (error) {
    return { code: 500, msg: "更新失败" };
  }
};
